package vn.smartoffice.promail.actor;

import java.util.Timer;
import java.util.TimerTask;

import javax.annotation.PostConstruct;
import javax.mail.Folder;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import vn.smartoffice.promail.actor.event.EmailCheck;
import vn.smartoffice.promail.actor.event.EmailConnect;
import vn.smartoffice.promail.actor.event.EmailDisconnect;
import vn.smartoffice.promail.actor.event.EmailState;
import vn.smartoffice.promail.actor.event.EmailTimer;
import akka.actor.PoisonPill;
import akka.actor.UntypedActor;

import com.sun.mail.imap.IMAPFolder;

@Component("emailActorBean")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class EmailActor extends UntypedActor {

	private final Logger logger = LoggerFactory.getLogger(EmailActor.class);

	@Autowired
	private Session session;

	private String username3 = "lienhe@sunrisesoft.vn";
	private String password3 = "Bac@1800gh";
	private String server3 = "123.30.108.184";
	private Store store;
	private IMAPFolder inbox;
	private Timer timer;
	private Callbackable<Integer> callback;

	@PostConstruct
	public void init() throws NoSuchProviderException {
		store = session.getStore("imaps");
	}

	@Override
	public void onReceive(Object message) throws Exception {
		if (message instanceof EmailConnect) {
			connect((EmailConnect) message);
		} else if (message instanceof EmailDisconnect) {
			disconnet((EmailDisconnect) message);
		} else if (message instanceof EmailCheck) {
			check((EmailCheck) message);
		} else if (message instanceof EmailTimer) {
			start((EmailTimer) message);
		}
	}

	private void connect(EmailConnect connect) throws MessagingException {
		store.connect(server3, username3, password3);
		inbox = (IMAPFolder) store.getFolder("INBOX");
		inbox.open(Folder.READ_ONLY);
		getSender().tell(EmailState.CONNECTED, getSelf());
	}

	private void disconnet(EmailDisconnect disconnect) throws MessagingException {
		getSender().tell(EmailState.DISICONNECT, null);
		getSelf().tell(PoisonPill.getInstance(), null);
		timer.cancel();
		inbox.close(false);
		store.close();
	}

	private void check(EmailCheck emailCheck) throws Exception {
		Integer newMsgCount = inbox.getNewMessageCount();
		callback.onCallback(newMsgCount);
		logger.info(String.valueOf(newMsgCount));
	}

	private void start(EmailTimer emailTimer) {
		this.callback = emailTimer.getCallback();
		timer = new Timer();
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				getSelf().tell(new EmailCheck(), null);
			}
		}, 1000, 1000 * 15);
	}

}
